package game.tileMap
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import game.tileMap.control.MapController;
	import game.tileMap.model.MapModel;
	import game.tileMap.view.background.MapBackground;

	/**
	 * map层用来处理地图中各种view和control的交互
	 * @author AresLee
	 * 
	 */	
	public class Map extends Sprite
	{
		[Embed(source="assets/art/test.png")]
		private var Img:Class;
		
		private var smap:Bitmap= new Img();
		
		private var mapC:MapController = new MapController();
		
		private var mapV:MapBackground = new MapBackground(200,200);
		
		private var mapM:MapModel = MapModel.getInstance();
		
		public function Map()
		{
			super();
			
			mapC.setOffset(0,0);
			
			mapV.setPos(100,50);
			
			mapV.currentBMD = smap.bitmapData;
			
			mapV.updateByOffset(0,0);
			
			addChild(mapV);
			
			addEventListener(Event.ADDED_TO_STAGE,onAddToStage);
		}
		private function onAddToStage(e:Event):void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
		}
		private function onKeyDown(e:KeyboardEvent):void
		{
			switch(e.keyCode){
				case Keyboard.LEFT:
					mapC.mapMove(-1,0);
					break;
				case Keyboard.RIGHT:
					mapC.mapMove(1,0);
					break;
				case Keyboard.UP:
					mapC.mapMove(0,-1);
					break;
				case Keyboard.DOWN:
					mapC.mapMove(0,1);
					break;
			}
			mapV.updateByOffset(mapM.offsetX,mapM.offsetY);
		}
	}
}